package com.song.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.song.common.enums.BusinessType;
import com.song.common.result.R;
import com.song.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.song.framework.annotation.RateLimiter;
import com.song.module.admin.entity.ApCart;
import com.song.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.song.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.service.IApCartService;
import com.song.module.admin.request.ApCartRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 菜篮子控制层
 *
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "菜篮子接口管理",tags = "菜篮子接口管理")
@RequestMapping("/admin/apCart" )
public class ApCartController {

    private final IApCartService iApCartService;

    /**
     * 分页查询菜篮子列表
     */
    @ApiOperation(value = "分页查询菜篮子列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApCart>> pageList(@RequestBody ApCartRequest apCartRequest) {
        return R.success(iApCartService.pageList(apCartRequest));
    }

    /**
     * 获取菜篮子详细信息
     */
    @ApiOperation(value = "获取菜篮子详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApCart> detail(@PathVariable("id") Long id) {
        return R.success(iApCartService.getById(id));
    }

    /**
     * 新增菜篮子
     */
    @Log(title = "新增菜篮子", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增菜篮子")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApCartRequest apCartRequest) {
        iApCartService.add(apCartRequest);
        return R.success();
    }

    /**
     * 修改菜篮子
     */
    @Log(title = "修改菜篮子", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改菜篮子")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApCartRequest apCartRequest) {
        iApCartService.update(apCartRequest);
        return R.success();
    }

    /**
     * 删除菜篮子
     */
    @Log(title = "删除菜篮子", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除菜篮子")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iApCartService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出菜篮子
    *
    * @param response response
    * @throws IOException IOException
    */
    @ApiOperation(value = "导出菜篮子")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody ApCartRequest apCartRequest) throws IOException {
        List<ApCart> list = iApCartService.list(apCartRequest);
        ExcelUtils.exportExcel(list, ApCart.class, "菜篮子信息", response);
    }
}
